<?php

namespace app\controller;

use app\BaseController;
use app\service\PermissionService;
use think\exception\ValidateException;
use think\facade\Request;

class Permission extends BaseController
{
    //合作商菜单列表tms
    public function permission_list()
    {
//        $param['uid']     = Request::param('uid/d', 0);
        $redisLink = linkRedis(8);
        $menuList = $redisLink->get('partner_menu_list');
        if ($menuList) {
            $permissionData = json_decode($redisLink->get('permission_list'), true);
        } else {
            $permissionData = PermissionService::getAllPermission();
            $redisLink->set('permission_list', json_encode($permissionData));
        }
        $newPermissionDatas = $permissionData;
        $permissionDatas = $this->cat_tree($permissionData);
        foreach ($permissionData as $k => $v) {
            if ($v['type'] == 2) {
                unset($permissionData[$k]);
            }
        }
        $tree = $this->cat_tree2($permissionData);
        $roleData = PermissionService::getAllRole();
        returnSuccess(200, '登录成功', ['permissionData' => array_values($permissionDatas), 'roleData' => $roleData, 'tree' => array_values($tree),
            'newPermissionDatas' => array_values($newPermissionDatas)]);
    }

    //合作商菜单列表wms
    public function permission_listwms()
    {
//        $param['uid']     = Request::param('uid/d', 0);
        $redisLink = linkRedis(8);
        $menuList = $redisLink->get('partner_menu_list');
        if ($menuList) {
            $permissionData = json_decode($redisLink->get('permission_list'), true);
        } else {
            $permissionData = PermissionService::getAllPermission();
            $redisLink->set('permission_list', json_encode($permissionData));
        }
        $newPermissionDatas = $permissionData;
        $permissionDatas = $this->cat_tree($permissionData);
        foreach ($permissionData as $k => $v) {
            if ($v['type'] == 2) {
                unset($permissionData[$k]);
            }
        }
        $tree = $this->cat_tree2($permissionData);
        $roleData = PermissionService::getAllRole();
        returnSuccess(200, '登录成功', ['permissionData' => array_values($permissionDatas), 'roleData' => $roleData, 'tree' => $tree,
            'newPermissionDatas' => array_values($newPermissionDatas)]);
    }

    protected function cat_tree($list, $parent_id = 0)
    {
        $temp = array();
        foreach ($list as $k => $v) {
            if ($v['pid'] == $parent_id) {
                $temp[$k] = $v;
                $res = $this->cat_tree($list, $v['id']);
                if (!empty($res)) {
                    $temp[$k]['children'] = array_values($res);
                }
            }
        }
        return $temp;
    }

    protected function cat_tree2($list, $parent_id = 0)
    {
        $temp = array();
        foreach ($list as $k => $v) {
            if ($v['pid'] == $parent_id) {
                $temp[$k] = $v;
                //定义控制器路径
                $res = $this->cat_tree2($list, $v['id']);
                if (!empty($res)) {
                    $temp[$k]['children'] = array_values($res);
                }
            }
        }
        return $temp;
    }

    //新增权限tms
    public function add_permission()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['status'] = Request::param('status/d', 1);
        //权限名称
        $param['name'] = Request::param('name/s', '');
        //模块名称
        $param['cname'] = Request::param('cname/s', '');
        //函数名称
        $param['fname'] = Request::param('fname/s', '');
        $param['pid'] = Request::param('pid/d', 0);
        $param['type'] = Request::param('type/d', 1);
        //是否无需权限 1 是 2 不是
        $param['is_unauth'] = Request::param('is_unauth/d', 1);
        //1 是  2 否
        $param['is_unlogin'] = Request::param('is_unlogin/d', 1);
        $param['ip'] = Request::ip();
        $param['system_ids'] = Request::param('system_ids/s', '');
        $param['btn_name'] = Request::param('btn_name/s', '');
        try {
            validate(\app\validate\Permission::class)->check([
                'name' => $param['name'],
                'cname' => $param['cname'],
                'fname' => $param['fname'],
                'pid' => $param['pid'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $resCode = PermissionService::add_permission($param);
        if ($resCode == 10000) {
            returnSuccess(200, '添加成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此权限已存在');
        } else {
            returnFail(205, '添加失败');
        }
    }

    //新增权限wms
    public function add_permissionwms()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['status'] = Request::param('status/d', 1);
        //权限名称
        $param['name'] = Request::param('name/s', '');
        //模块名称
        $param['cname'] = Request::param('cname/s', '');
        //函数名称
        $param['fname'] = Request::param('fname/s', '');
        $param['pid'] = Request::param('pid/d', 0);
        $param['type'] = Request::param('type/d', 1);
        //是否无需权限 1 是 2 不是
        $param['is_unauth'] = Request::param('is_unauth/d', 1);
        //1 是  2 否
        $param['is_unlogin'] = Request::param('is_unlogin/d', 1);
        $param['ip'] = Request::ip();
        $param['system_ids'] = Request::param('system_ids/s', '');
        $param['btn_name'] = Request::param('btn_name/s', '');
        try {
            validate(\app\validate\Permission::class)->check([
                'name' => $param['name'],
                'cname' => $param['cname'],
                'fname' => $param['fname'],
                'pid' => $param['pid'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $resCode = PermissionService::add_permission($param);
        if ($resCode == 10000) {
            returnSuccess(200, '添加成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此权限已存在');
        } else {
            returnFail(205, '添加失败');
        }
    }

    //修改权限tms
    public function modify_permission()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['status'] = Request::param('status/d', 1);
        //权限名称
        $param['name'] = Request::param('name/s', '');
        //模块名称
        $param['cname'] = Request::param('cname/s', '');
        //函数名称
        $param['fname'] = Request::param('fname/s', '');
        $param['pid'] = Request::param('pid/d', 0);
        $param['type'] = Request::param('type/d', 1);
        //是否无需权限 1 是 2 不是
        $param['is_unauth'] = Request::param('is_unauth/d', 1);
        //1 是  2 否
        $param['is_unlogin'] = Request::param('is_unlogin/d', 1);
        $permission_id = Request::param('permission_id/d', 0);
        $param['system_ids'] = Request::param('system_ids/s', '');
        $param['ip'] = Request::ip();
        $param['btn_name'] = Request::param('btn_name/s', '');
        try {
            validate(\app\validate\Permission::class)->check([
                'name' => $param['name'],
                'cname' => $param['cname'],
                'fname' => $param['fname'],
                'pid' => $param['pid'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        if ($permission_id == 0) {
            returnFail(21204, '请求参数不正确');
        }
        $resCode = PermissionService::modify_permission($param, $permission_id);
        if ($resCode == 10000) {
            returnSuccess(200, '修改成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此权限不存在');
        } else {
            returnFail(205, '修改失败');
        }
    }

    //修改权限wms
    public function modify_permissionwms()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['status'] = Request::param('status/d', 1);
        //权限名称
        $param['name'] = Request::param('name/s', '');
        //模块名称
        $param['cname'] = Request::param('cname/s', '');
        //函数名称
        $param['fname'] = Request::param('fname/s', '');
        $param['pid'] = Request::param('pid/d', 0);
        $param['type'] = Request::param('type/d', 1);
        //是否无需权限 1 是 2 不是
        $param['is_unauth'] = Request::param('is_unauth/d', 1);
        //1 是  2 否
        $param['is_unlogin'] = Request::param('is_unlogin/d', 1);
        $permission_id = Request::param('permission_id/d', 0);
        $param['system_ids'] = Request::param('system_ids/s', '');
        $param['ip'] = Request::ip();
        $param['btn_name'] = Request::param('btn_name/s', '');
        try {
            validate(\app\validate\Permission::class)->check([
                'name' => $param['name'],
                'cname' => $param['cname'],
                'fname' => $param['fname'],
                'pid' => $param['pid'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        if ($permission_id == 0) {
            returnFail(21204, '请求参数不正确');
        }
        $resCode = PermissionService::modify_permission($param, $permission_id);
        if ($resCode == 10000) {
            returnSuccess(200, '修改成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此权限不存在');
        } else {
            returnFail(205, '修改失败');
        }
    }

    //删除权限tms
    public function del_permission()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $permission_id = Request::param('permission_id/d', 0);
        $param['ip'] = Request::ip();
        if ($permission_id == 0) {
            returnFail(21204, '请求参数不正确');
        }
        $resCode = PermissionService::del_permission($param, $permission_id);
        if ($resCode == 10000) {
            returnSuccess(200, '删除成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此权限已被删除');
        } else {
            returnFail(205, '删除失败');
        }
    }

    //删除权限wms
    public function del_permissionwms()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $permission_id = Request::param('permission_id/d', 0);
        $param['ip'] = Request::ip();
        if ($permission_id == 0) {
            returnFail(21204, '请求参数不正确');
        }
        $resCode = PermissionService::del_permission($param, $permission_id);
        if ($resCode == 10000) {
            returnSuccess(200, '删除成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此权限已被删除');
        } else {
            returnFail(205, '删除失败');
        }
    }

    public function log()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $res = PermissionService::log($param);
        returnSuccess(200, '获取成功', $res);
    }
}
